﻿using UnityEngine;
using System.Collections;

public class WallBounceSideScroller : MonoBehaviour {

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	void OnTriggerEnter2D(Collider2D other) {
		if (other.tag == "Player") {
			//Debug.Log ("TAG");
			Vector2 direction = Vector2.zero;
			Vector2 vel = other.rigidbody2D.velocity;
			if (this.tag == "LeftWallBouncer") {
				direction = new Vector2(1f,0f);
				vel.x = 0f;
			}
			else if (this.tag == "RightWallBouncer") {
				direction = new Vector2(-1f, 0f);
				vel.x = 0f;
			}
			else if (this.tag == "CeilingBouncer") {
				direction = new Vector2(0f,-1f);
				vel.y = 0f;
			}
			else if (this.tag == "FloorBouncer") {
				direction = new Vector2(0f,1f);
				vel.y = 0f;
			}
			other.rigidbody2D.velocity = vel;
			other.rigidbody2D.AddForce(direction * 150f);
		}
		else if (other.tag == "Egg" || other.tag == "Enemy") {
			Vector2 direction = Vector2.zero;
			Vector2 vel = other.rigidbody2D.velocity;
			float currentX = 0f;
			bool canUse = false;
			if (this.tag == "LeftWallBouncer") {
				direction = new Vector2(1f,0f);
				currentX = vel.x * -1f;
				vel.x = 0f;
				
				Vector3 vec = other.gameObject.transform.localScale;
				if (other.tag == "Egg") {
					//vec.x = 2f;
				}
				else {
					vec.x = -1f;
				}
				other.gameObject.transform.localScale = vec;
				
				
				canUse = true;
			}
			else if (this.tag == "RightWallBouncer") {
				direction = new Vector2(-1f, 0f);
				currentX = vel.x;
				vel.x = 0f;
				
				Vector3 vec = other.gameObject.transform.localScale;
				if (other.tag == "Egg") {
					//vec.x = -2f;
				}
				else {
					vec.x = 1f;
				}
				other.gameObject.transform.localScale = vec;
				
				canUse = true;
			}
			/*
			else if (this.tag == "CeilingBouncer") {
				direction = new Vector2(0f,-1f);
				vel.y = 0f;
			}
			else if (this.tag == "FloorBouncer") {
				direction = new Vector2(0f,1f);
				vel.y = 0f;
			}
			*/
			if (canUse) {
				other.rigidbody2D.velocity = vel;
				other.rigidbody2D.AddForce(direction * currentX * 50f);
			}
		}
	}

}
